package com.example.learnspringboot05web.controller;

import com.example.learnspringboot05web.exception.UserNotExistException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Map;

//@RestController

//注意这里为了配合使用 Thymeleaf ， 所以该用 @Controller，下面访问 success 之后会自动显示 success.html 页面
@Controller
public class HellowordController {

//    @RequestMapping({"/","/login.html"})// 访问当前目录下的 templates 或者 templates/index.html
//    public String index(){ // 对应 MyMvcConfig 里的 webMvcConfigurer 方法
//        return "login";
//    }

    @ResponseBody
    @RequestMapping("/hello")
    public String hello(){
        return "hello web";
    }

    @ResponseBody
    @RequestMapping("/hello2")

    public String hello2(@RequestParam("user") String user){
        if ("aaa".equals(user)){
            throw new UserNotExistException();
        }
        return "hello web";
    }
    @RequestMapping("/success")
    public String success(Map<String,Object> map){
        map.put("java","<h1>springboot-Thymeleaf</h1>");
        map.put("users", Arrays.asList("test1","test2","test3"));
        //classpath:/templates/success.html
        return "success";
    }
}
